import json
import re
import uuid

from core.utils.analysis_platform_id_tool import AnalysisPlatformIdTool


class AnalysisResponseService:
    def __init__(self):
        pass

    @staticmethod
    def analysis_list_first_response(response, category):
        """
        解析 国内、国际、军事、航空、政务、公益 类型的响应结果
        :param response: 响应结构
        :param category: 新闻类型
        :return: 返回结果
        """
        # 去除 data_callback( 和最后一个 )
        response_str = (response.text.replace('data_callback(', '')
                        .replace('datalist_gyrd(', '')
                        .replace(')', ''))
        # 将字符串转换为 Json
        response_list = json.loads(response_str)
        data_list = list()
        for data in response_list:
            news_title = data['title']
            href = data['docurl']
            follower = data['tienum']
            pic_href = data['imgurl']
            news_id = AnalysisPlatformIdTool.re_get_id(href)

            keywords = data['keywords']
            keywords_str = ""
            for keyword in keywords:
                keywords_str += keyword['keyname'] + "\\"
            keywords_str.rstrip("\\")

            # print(news_id, news_title, follower, pic_href, category, href, keywords_str)
            data_list.append(
                {'news_id': news_id, 'news_title': news_title, 'follower': follower,
                 'pic_href': pic_href, 'category': category,
                 'href': href, 'keywords': keywords_str})
        return data_list